<template>
  <el-dialog v-model="dialogVisible" :destroy-on-close="true" size="450px" title="查看详情">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="dialogProps.isView"
      :model="dialogProps.row"
      :hide-required-asterisk="dialogProps.isView"
    >
      <el-form-item label="主键" prop="id">
        <el-input v-model="dialogProps.row!.id" clearable></el-input>
      </el-form-item>
      <el-form-item label="用户账号" prop="username">
        <el-input v-model="dialogProps.row!.username" clearable></el-input>
      </el-form-item>
      <el-form-item label="操作系统" prop="os">
        <el-input v-model="dialogProps.row!.os" clearable></el-input>
      </el-form-item>
      <el-form-item label="设备名" prop="device">
        <el-input v-model="dialogProps.row!.device" clearable></el-input>
      </el-form-item>
      <el-form-item label="浏览器类型" prop="browser">
        <el-input v-model="dialogProps.row!.browser" clearable></el-input>
      </el-form-item>
      <el-form-item label="ip地址" prop="ip">
        <el-input v-model="dialogProps.row!.ip" clearable></el-input>
      </el-form-item>
      <el-form-item label="操作类型" prop="operType">
        <el-input v-model="dialogProps.row!.operType" clearable></el-input>
      </el-form-item>
      <el-form-item label="登录时间" prop="createTime">
        <el-input v-model="dialogProps.row!.createTime" clearable></el-input>
      </el-form-item>
      <el-form-item label="修改时间" prop="updateTime">
        <el-input v-model="dialogProps.row!.updateTime" clearable></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible = false"> 取消 </el-button>
      <el-button v-show="!dialogProps.isView" type="primary" @click="handleSubmit"> 确定 </el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="RoleDialog">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { Operation } from "@/api/interface";

const rules = reactive({
  username: [{ required: true, message: "请输入角色名称" }]
});

interface DialogProps {
  title: string;
  isView: boolean;
  row: Partial<Operation.ResDepartmentList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const dialogVisible = ref(false);
const dialogProps = ref<DialogProps>({
  isView: false,
  title: "",
  row: {}
});

// 接收父组件传过来的参数
const acceptParams = (params: DialogProps) => {
  dialogProps.value = params;
  dialogVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await dialogProps.value.api!(dialogProps.value.row);
      ElMessage.success({ message: `${dialogProps.value.nickName}角色成功！` });
      dialogProps.value.getTableList!();
      dialogVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
